<?php

namespace App\Admin\Controllers;

use App\Models\ProjectModel;
use App\Admin\Models\CustomerModel;
use App\Models\UserModel;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

use Illuminate\Http\Request;

class ProjectsController extends Controller
{
    use ModelForm;
    protected $_method=0;
    protected $_discreation=0;
    protected $_showself=1;


    /**
     * Index interface.
     *
     * @return Content
     */
    
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('我的项目');
            $content->description('');
//            $method = isset($_GET['method'])?$_GET['method']:0;
            $content->body($this->grid());
        });
    }
    
    
    
    public function showMethods(Request $request){
        $this->_method =  $request->route('method');
        $this->_discreation = 1;
        $this->_showself = 0;
        return Admin::content(function (Content $content) {
            
            switch($this->_method){
                case 1:
                   $content->header('钻探');
                    break;
                case 2:
                   $content->header('取样');
                    break;
                case 3:
                   $content->header('分析');
                    break;
                case 4:
                   $content->header('修复');
                    break;
            }
            $content->description('');
//        $method = isset($_GET['method'])?$_GET['method']:0;
//            var_dump($request);
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('项目');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }
    
    public function save(){
//        var_dump($_POST);exit;
        $this->form()->store();
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增项目');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
                

    
     public function grid()
    {
//        $this->_method=$method;
        return Admin::grid(ProjectModel::class, function (Grid $grid) {
            
             $grid->resource('admin/projects');

//            $grid->id('ID')->sortable();
            $grid->column('place','所在地')->sortable();
            $grid->column('projectname','项目名称');
            $grid->column('assignerid','项目经理')->value(function($userId){
                $oManager = new ProjectModel(); 
                $oM = $oManager->getManager($userId); 
                return $oM->name;
            });
            $grid->column('mode','项目')->value(function($mId){
                $sA = "";
                switch($mId){
                    case '1':
                        $sA = '钻探';
                        break;
                    case '2':
                        $sA = '取样';
                        break;
                    case '3':
                        $sA = '分析';
                        break;
                    case '4':
                        $sA = '修复';
                        break;   
                }
                return $sA;
            });
            $grid->column('assigndate','分配时间');
                    
            
            $grid->filter(function ($filter) {
    
                  // 设置created_at字段的范围查询
                     $filter->between('startdate', '开始时间')->datetime();
             });
//             var_dump($this->_method);
             $method = $this->_method;
             if($method>0){
                      $grid->model()->where('mode','=',$method);
             } 
             $aMe = Admin::user();
//             var_dump($aMe->id);exit;
             if($this->_showself==1){
                $grid->model()->where('assigneeid','=',$aMe->id);
             }
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableBatchDeletion();
            if($this->_discreation==1){
            $grid->disableCreation();
            }
//            $grid->disableActions();
        });
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ProjectModel::class, function (Form $form) {
            $id = Admin::user()->id;
             $oP = new ProjectModel();
             $aPs = $oP->getManagerNames($id);
             $oC = new CustomerModel();
             $aCs = $oC->getCustomerList();
//                var_dump($aPs);
            $form->text('place','所在地')->rules('required');     
            $form->select('company','公司名称')->options($aCs);          
            $form->text('projectname','项目名称')->rules('required');
            $form->text('address','地址')->rules('required');
            $form->select('mode','当前任务')->options(array('1'=>'钻探','2'=>'取样','3'=>'分析','4'=>'修复'));
            $form->date('startdate','开始时间')->rules('required');
            $form->select('assignerid', '项目经理')->options($aPs);
//            $form->setAction('/admin/projects/save');
            $form->text('latitude','纬度');
            $form->text('longitude','经度');
            $form->map('latitude', 'longitude','位置')->useTencentMap();
            $form->hidden('assigneeid')->value($id);

            $form->model()->timestamps = false;
            

//            $form->display('created_at', 'Created At');
//            $form->display('updated_at', 'Updated At');
            
            $form->saving(function (Form $form) {
                if ($form->projectname && $form->model()->projectname == $form->projectname) {
                    return false;
                }
            });

        });
    }
   
}
